<?php

class ProveedoresController extends AppController {

    public $name = 'Proveedores';
    public $uses = array('Proveedor');

    function index() {
        $proveedores = $this->paginate('Proveedor');
        $this->set(compact('proveedores'));
    }

    function ver($id = null) {
        $this->Proveedor->id = $id;
        $this->set('proveedor', $this->Proveedor->read());
        $this->set('id', $id);
    }

    function agregar() {
        if (!empty($this->data)) {
            if ($this->Proveedor->save($this->data)) {
                $this->Session->setFlash('Proveedor agregado exitosamente.');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Error al agregar proveedor');
                $this->redirect(array('action' => 'index'));
            }
        }
        //select multiple
        $contactos = $this->Proveedor->Contacto->find('list', array('fields' => array('id', 'nombre_completo')));
        $this->set(compact('contactos'));
        $medios_pagos = $this->Proveedor->MedioPago->find('list', array('fields' => array('id', 'nombre')));
        $this->set(compact('medios_pagos'));
    }

    function borrar($id) {
        if ($this->Proveedor->delete($id)) {
            $this->Session->setFlash('Proveedor eliminado exitosamente.');
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash('Error al borrar proveedor');
            $this->redirect(array('action' => 'index'));
        }
    }

    function editar($id) {
        $this->Proveedor->id = $id;
        if (empty($this->data)) {
            $this->data = $this->Proveedor->read();
            //select multiple //se seleccionan automaticamente los relacionados
            $contactos = $this->Proveedor->Contacto->find('list', array('fields' => array('id', 'nombre_completo')));
            $this->set(compact('contactos'));
            $medios_pagos = $this->Proveedor->MedioPago->find('list', array('fields' => array('id', 'nombre')));
            $this->set(compact('medios_pagos'));
            $this->set('id', $id);
        } else {
            if ($this->Proveedor->save($this->data)) {
                $this->Session->setFlash('Proveedor editado exitosamente.');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Error al editar proveedor');
                $this->redirect(array('action' => 'index'));
            }
        }
    }

}

?>
